package com.yuanfengerp.inventory.service.impl;


import com.yuanfengerp.inventory.dao.IWarehouseTransferDao;
import com.yuanfengerp.inventory.entity.WarehouseTransfer;
import com.yuanfengerp.inventory.service.IWarehouseTransferService;
import com.yuanfengerp.publicUtil.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseTransferServiceImpl implements IWarehouseTransferService {
    @Autowired
    private IWarehouseTransferDao iwtd;

    //根据多个查询条件查询所有库存调拨单
    @Override
    public Result<List<WarehouseTransfer>> queryWarehouseTransfer(Map<String, Object> params) {
        List<WarehouseTransfer> warehouseTransfers = iwtd.queryWarehouseTransfer(params);
        if (!warehouseTransfers.isEmpty()) {
            return Result.success(warehouseTransfers);
        }
        return Result.failure(400, "暂无数据");
    }
    //新增库存调拨单
    @Override
    public Result<String> addWarehouseTransfer(WarehouseTransfer warehouseTransfer) {
        warehouseTransfer.setTransfer_operating_time(new Date());
        warehouseTransfer.setAudit_time(new Date());
        int i = iwtd.addWarehouseTransfer(warehouseTransfer);
        if (i > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failure(400, "添加失败");
        }
    }
    //根据ID查询库存调拨单
    @Override
    public Result<WarehouseTransfer> queryWarehouseTransferById(String warehouse_document_number) {
        WarehouseTransfer warehouseTransferById = iwtd.queryWarehouseTransferById(warehouse_document_number);
        if (warehouseTransferById != null) {
            return Result.success(warehouseTransferById);
        } else {
            return Result.failure(400, "查询失败");
        }
    }

    //修改库存调拨单
    @Override
    public Result<String> updateWarehouseTransfer(WarehouseTransfer warehouseTransfer) {
        int i = iwtd.updateWarehouseTransfer(warehouseTransfer);
        if (i > 0) {
            return Result.success("修改成功");
        } else {
            return Result.failure(400, "修改失败");
        }
    }
    //删除库存调拨单
    @Override
    public Result<String> deleteWarehouseTransfer(String warehouse_document_number) {
        int i = iwtd.deleteWarehouseTransfer(warehouse_document_number);
        if (i > 0) {
            return Result.success("删除成功");
        } else {
            return Result.failure(400, "删除失败");
        }
    }
    //审核通过
    @Override
    public Result<String> updateWarehouseTransferAgree(String warehouse_document_number) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝,3——部分收货，4——收货完成
        System.out.println(warehouse_document_number);
        int i = iwtd.updateWarehouseTransferAgree(warehouse_document_number);
        if (i > 0) {
            return Result.success(warehouse_document_number + "审核通过");
        } else {
            return Result.failure(400, "审核状态为" + 1 + "，请勿重复审核");
        }
    }
    //审核不通过
    @Override
    public Result<String> updateWarehouseTransferRefuse(String warehouse_document_number) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝,3——部分收货，4——收货完成
        System.out.println(warehouse_document_number);
        int i = iwtd.updateWarehouseTransferRefuse(warehouse_document_number);
        if (i > 0) {
            return Result.success(warehouse_document_number + "审核拒绝");
        } else {
            return Result.failure(400, "审核状态为" + 2 + "，请勿重复审核");
        }
    }

    @Override
    public Result<String> updateWarehouseTransferPartialReceipt(String warehouse_document_number) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝,3——部分收货，4——收货完成

        int i = iwtd.updateWarehouseTransferPartialReceipt(warehouse_document_number);
        if (i > 0){
            return Result.success("部分收货");
        }else {
            return Result.failure(400,"状态为" + 3 + "，请勿重复修改状态");
        }
    }

    @Override
    public Result<String> updateWarehouseTransferReceipt(String warehouse_document_number) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝,3——部分收货，4——收货完成
        int i = iwtd.updateWarehouseTransferReceived(warehouse_document_number);
        if (i > 0){
            return Result.success("收货完成");
        }else {
            return Result.failure(400,"状态为" + 4 + "，请勿重复修改状态");
        }
    }
}
